package com.example.im.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.im.R;
import com.example.im.model.bean.InvationInfo;
import com.example.im.model.bean.UserInfo;
import com.example.im.utils.Constant;

import java.util.ArrayList;
import java.util.List;

//邀请信息列表页面的适配器
public class InviteAdapter extends BaseAdapter {
    private Context mContext;
    private List<InvationInfo> mInvationInfos = new ArrayList<>();
    private OnInviteListener mOninviteListener;
    private InvationInfo invationInfo;

    public InviteAdapter(Context context,OnInviteListener onInviteListener) {
        mContext = context;
        mOninviteListener = onInviteListener;
    }

    //刷新数据的方法
    public void refresh(List<InvationInfo> invationInfos){

        if (invationInfos !=null && invationInfos.size()>=0){
            //每次进来之前需要对集合进行清空
            mInvationInfos.clear();

            mInvationInfos.addAll(invationInfos);
            //刷新
            notifyDataSetChanged();
        }
    }
    @Override//集合数据的长度
    public int getCount() {
        return mInvationInfos == null? 0: mInvationInfos.size();
    }

    @Override//集合里哪一条数据（数据）
    public Object getItem(int position) {
        return mInvationInfos.get(position);
    }

    @Override//集合里哪一条数据（位置）
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        //1 获取或创建viewHolder
        ViewHolder holder = null;
        if (view == null){
            holder = new ViewHolder();
            view = View.inflate(mContext, R.layout.item_invite,null);

            holder.name = view.findViewById(R.id.tv_invite_name);
            holder.reason = view.findViewById(R.id.tv_invite_reason);
            holder.accept = view.findViewById(R.id.bt_invite_accept);
            holder.reject = view.findViewById(R.id.bt_invite_reject);

            view.setTag(holder);

        }else {
            holder = (ViewHolder) view.getTag();
        }
        //2 获取当前item数据
        invationInfo = mInvationInfos.get(position);

        //3 显示当前item数据
        UserInfo userInfo = invationInfo.getUser();

        if (userInfo != null){//联系人
            //名称展示
            holder.name.setText(invationInfo.getUser().getName());

            holder.accept.setVisibility(View.GONE);
            holder.reject.setVisibility(View.GONE);

            //原因
            if (invationInfo.getStatus() == InvationInfo.InvitationStatus.NEW_INVITE){//新的邀请
                if (invationInfo.getReason() == null){
                    holder.reason.setText("添加好友");
                }else {
                    holder.reason.setText(invationInfo.getReason());
                }
                holder.accept.setVisibility(View.VISIBLE);
                holder.reject.setVisibility(View.VISIBLE);

            }else if (invationInfo.getStatus() == InvationInfo.InvitationStatus.INVITE_ACCEPT){//接受邀请
                if (invationInfo.getReason() == null){
                    holder.reason.setText("接受邀请");
                }else {
                    holder.reason.setText(invationInfo.getReason());
                }

            }else if (invationInfo.getStatus() == InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER){//邀请被接受
                if (invationInfo.getReason() == null){
                    holder.reason.setText("邀请被接受");
                }else {
                    holder.reason.setText(invationInfo.getReason());
                }
            }

            //按钮的处理
            holder.accept.setOnClickListener(new View.OnClickListener() {//接受事件监听
                @Override
                public void onClick(View view) {
                    mOninviteListener.onAccept(invationInfo);
                }
            });
            holder.reject.setOnClickListener(new View.OnClickListener() {//拒绝事件监听
                @Override
                public void onClick(View view) {
                    mOninviteListener.onReject(invationInfo);

                }
            });

        }else {//群组
            //显示名称
            holder.name.setText(invationInfo.getGroup().getInvatePerson());
            //统一隐藏button
            holder.accept.setVisibility(View.GONE);
            holder.reject.setVisibility(View.GONE);
            //显示原因
            switch(invationInfo.getStatus()){

                // 您的群申请已经被接受
                case GROUP_APPLICATION_ACCEPTED:
                    holder.reason.setText("您的群申请已经被接受");
                    break;

                //  您的群邀请已经被接收
                case GROUP_INVITE_ACCEPTED:
                    holder.reason.setText("您的群邀请已经被接收");
                    break;

                // 你的群申请已经被拒绝
                case GROUP_APPLICATION_DECLINED:
                    holder.reason.setText("你的群申请已经被拒绝");
                    break;

                // 您的群邀请已经被拒绝
                case GROUP_INVITE_DECLINED:
                    holder.reason.setText("您的群邀请已经被拒绝");


                    break;

                // 您收到了群邀请
                case NEW_GROUP_INVITE:
                    holder.reason.setText("您收到了群邀请");
                    holder.accept.setVisibility(View.VISIBLE);
                    holder.reject.setVisibility(View.VISIBLE);
                    //接受邀请
                    holder.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOninviteListener.onInviteAccept(invationInfo);

                        }
                    });

                    //拒绝邀请
                    holder.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOninviteListener.onApplicationReject(invationInfo);
                        }
                    });
                    break;

                // 您收到了群申请
                case NEW_GROUP_APPLICATION:
                    holder.reason.setText("您收到了群申请");
                    holder.accept.setVisibility(View.VISIBLE);
                    holder.reject.setVisibility(View.VISIBLE);

                    //接受申请
                    holder.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOninviteListener.onApplicationAccept(invationInfo);
                        }
                    });

                    //拒绝申请
                    holder.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mOninviteListener.onApplicationReject(invationInfo);
                        }
                    });
                    break;

                // 你接受了群邀请
                case GROUP_ACCEPT_INVITE:
                    holder.reason.setText("你接受了群邀请");
                    break;

                // 您批准了群申请
                case GROUP_ACCEPT_APPLICATION:
                    holder.reason.setText("您批准了群申请");
                    break;

                // 你拒绝了群邀请
                case GROUP_REJECT_INVITE:
                    holder.reason.setText("你拒绝了群邀请");
                    break;

                // 您拒绝了群申请
                case GROUP_REJECT_APPLICATION:
                    holder.reason.setText("您拒绝了群申请");
                    break;
            }
        }
        //4 返回view

        return view;
    }

    private class ViewHolder{
        private TextView name;
        private TextView reason;

        private Button accept;
        private Button reject;
    }

    public interface OnInviteListener{
        //联系人接受按钮点击事件
        void onAccept(InvationInfo invationInfo);
        //联系人拒绝按钮点击事件
        void onReject(InvationInfo invationInfo);

        //接收邀请按钮处理
        void onInviteAccept(InvationInfo invationInfo);
        //拒绝邀请按钮处理
        void onInviteReject(InvationInfo invationInfo);

        //接收申请按钮处理
        void onApplicationAccept(InvationInfo invationInfo);
        //拒绝申请按钮处理
        void onApplicationReject(InvationInfo invationInfo);
    }
}
